package com.timedomain.him.remake.ui;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import androidx.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.timedomain.him.remake.R;
import com.timedomain.him.remake.utils.BaseActivity;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class ContributorActivity extends BaseActivity {

    HashMap<String,ContributerData> contributerDatas = new HashMap<>();
    List<ContributerData> contributerDataList = new ArrayList<>();
    private void addContributer(String key,ContributerData data){
        contributerDatas.put(key,data);
        contributerDataList.add(data);
    }

    public void loadContributerData(){
        addContributer("liuliuchan",
                new ContributerData(R.drawable.contributer_liuliuchan,"文案","六六酱","一个很懒、拖延症爆棚，但是啥都会点啥都想学的梦女。",
                        new ContributerLink[]{
                                new ContributerLink("小红书","https://www.xiaohongshu.com/user/profile/58dd275882ec394e4902aef8?xhsshare=CopyLink&appuid=58dd275882ec394e4902aef8&apptime=1695607928"),
                                new ContributerLink("Bilibili","https://b23.tv/GPRmPdK")
                        }));
        addContributer("luojianghuai",
                new ContributerData(R.drawable.contributer_luojianghuai,"配音","骆江淮","养成型CV，懒散狐狐一只(*/ω＼*)，日常活跃于克拉克拉，不定期直播。",
                        new ContributerLink[]{
                                new ContributerLink("猫耳FM","https://www.missevan.com/18460357/"),
                                new ContributerLink("克拉克拉","https://www.hongdoufm.com/PcLive/index/detail?id=1860862759571292276"),
                                new ContributerLink("Bilibili","https://space.bilibili.com/312703054"),
                        }));

        addContributer("zyfdroid",
                new ContributerData(R.drawable.contribute_zyfdroid,"程序 & 策划","ZYFDroid","致敬每一个理想主义者 (比如官方)",
                        new ContributerLink[]{
                                new ContributerLink("项目主页","https://gitee.com/ZYFDroid/him-remake"),
                                new ContributerLink("Bilibili","https://b23.tv/gEcSxAV"),
                                new ContributerLink("小红书","https://www.xiaohongshu.com/user/profile/6470cc1d0000000012036daa?xhsshare=CopyLink&appuid=6470cc1d0000000012036daa&apptime=1695609587"),
                                new ContributerLink("Github主页","https://github.com/ZYFDroid/"),
                                new ContributerLink("Gitee主页","https://gitee.com/ZYFDroid/")
                        }));
        addContributer("soundeffectlab",
                new ContributerData(R.drawable.website,"効果音素材网站","効果音ラボ","商用無料、報告不用の効果音素材をダウンロード",
                        new ContributerLink[]{
                                new ContributerLink("主页地址: soundeffect-lab.info","https://soundeffect-lab.info/")
                        }));
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contributor);
        loadContributerData();
        ListView lv = findViewById(R.id.listContributer);
        lv.setAdapter(new ContributerAdapter(this));
        lv.setDividerHeight(0);
        lv.addFooterView(getLayoutInflater().inflate(R.layout.list_contributer_footer,null,false));
        lv.setOnItemClickListener((parent, view, position, id) -> new ContributerCardDialog(ContributorActivity.this,contributerDataList.get(position)).show());
    }


    static class ContributerCardDialog extends Dialog{

        private ContributerData data;
        private ListView dlgContributerListLinks;
        private TextView dlgContributerSign;
        private TextView dlgContributerlblType;
        private TextView dlgContributerlblAvator;
        private ImageView dlgContributerImgAvator;
        private void initUI(){
            dlgContributerListLinks = (ListView)findViewById(R.id.dlgContributerListLinks);
            dlgContributerSign = (TextView)findViewById(R.id.dlgContributerSign);
            dlgContributerlblType = (TextView)findViewById(R.id.dlgContributerlblType);
            dlgContributerlblAvator = (TextView)findViewById(R.id.dlgContributerlblAvator);
            dlgContributerImgAvator = (ImageView)findViewById(R.id.dlgContributerImgAvator);
        }

        public ContributerCardDialog(@NonNull @NotNull Context context,ContributerData data) {
            super(context);
            setContentView(R.layout.dialog_contributer_card);
            this.data = data;
            initUI();
            showData();
            setCancelable(true);
            getWindow().setBackgroundDrawableResource(android.R.color.transparent);
            getWindow().setLayout(-1,-2);
        }

        private void showData(){
            dlgContributerImgAvator.setImageResource(data.avatorDrawable);
            dlgContributerlblAvator.setText(data.cname);
            dlgContributerlblType.setText(data.type);
            dlgContributerSign.setText(data.sign);
            dlgContributerListLinks.setAdapter(new LinkAdapter());
        }

        class LinkAdapter extends ArrayAdapter<ContributerLink>{

            public LinkAdapter() {
                super(ContributerCardDialog.this.getContext(), R.layout.list_contributer_link,data.links);
            }

            @NonNull
            @NotNull
            @Override
            public View getView(int position, @androidx.annotation.Nullable @Nullable View convertView, @NonNull @NotNull ViewGroup parent) {
                if(convertView == null){
                    convertView = getLayoutInflater().inflate(R.layout.list_contributer_link,parent,false);

                }
                Button btn = convertView.findViewById(R.id.listContributerLinkItem);
                ContributerLink data = getItem(position);
                btn.setTag(data.url);
                btn.setText(data.displayName);
                btn.setOnClickListener(v -> gotoUrl(v.getTag().toString()));
                ViewGroup.LayoutParams lp = convertView.getLayoutParams();
                lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
                convertView.setLayoutParams(lp);
                return convertView;
            }
        }

        public void gotoUrl(String str) {
            try {
                Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(str));
                getContext().startActivity(i);
            }catch (Exception ex){
                Toast.makeText(getContext(), "无法打开链接: "+str, Toast.LENGTH_SHORT).show();
            }
        }
    }


    class ContributerAdapter extends ArrayAdapter<ContributerData>{

        public ContributerAdapter(@NonNull Context context) {
            super(context, R.layout.list_contributer,contributerDataList);
        }

        @NonNull
        @NotNull
        @Override
        public View getView(int position, @androidx.annotation.Nullable @Nullable View convertView, @NonNull @NotNull ViewGroup parent) {
            if(convertView == null){
                convertView = getLayoutInflater().inflate(R.layout.list_contributer,parent,false);
            }
            ContributerData data = getItem(position);
            ((ImageView) convertView.findViewById(R.id.listContributerImgAvator)).setImageResource(data.avatorDrawable);
            ((TextView) convertView.findViewById(R.id.listContributerNickName)).setText(data.cname);
            ((TextView) convertView.findViewById(R.id.listContributerSign)).setText(data.sign);
            ((TextView) convertView.findViewById(R.id.listContributerType)).setText(data.type);
            ViewGroup.LayoutParams lp = convertView.getLayoutParams();
            lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
            convertView.setLayoutParams(lp);
            return convertView;
        }
    }

    public void gotoOpensourcePage(View view) {
        gotoUrl("https://gitee.com/ZYFDroid/him-remake");
    }

    public void gotoCCBYLicense(View view) {
        gotoUrl("http://creativecommons.org/licenses/by/4.0/");
    }

    public void gotoUrl(String str) {
        try {
            Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(str));
            startActivity(i);
        }catch (Exception ex){
            showToast("无法打开链接："+str);
        }
    }

    public static class ContributerData{

        public int avatorDrawable;
        public String type;
        public String cname;
        public String sign;
        public ContributerLink[] links;

        public ContributerData(int avatorDrawable,String type ,String cname, String sign, ContributerLink[] links) {
            this.avatorDrawable = avatorDrawable;
            this.type = type;
            this.cname = cname;
            this.sign = sign;
            this.links = links;
        }

    }

    public static class ContributerLink{

        public ContributerLink(String displayName, String url) {
            this.displayName = displayName;
            this.url = url;
        }

        public String displayName;
        public String url;

    }
}
